<?php
namespace Admin\Controller;
use Think\Controller;
class SeckillController extends AdminController {
    public function index(){
        $this->assign('title','秒杀列表');
        $Model = new \Think\Model();
        $list = $Model->field(array('g.id as gid,s.id as sid ,s.begin_time,s.end_time,s.num,s.seck_pri,g.price,g.name as gname,s.is_lose,s.is_open'))->table('shopping_seckill as s,shopping_goods as g')->where("g.id=s.goods_id")->select();
//        dump($list);die;

        $ms = M('seckill');
        $res = $ms->select();
        $today = mktime();//即时时间戳
        foreach($res as $k =>$val){
            $end_time = strtotime($res[$k]['end_time']);
            $id = $res[$k]['id'];
            if($end_time<=$today){
                $data['is_lose'] = 0;//0过期
                $res1 = $ms->where("id='$id'")->save($data);
            }else{
                $data['is_lose'] = 1;//1未过期
                $res1 = $ms->where("id='$id'")->save($data);
            }
        }
        $res = $ms->select();
//        $this->assign('today',$today);
        $this->assign('list',$list);
        $this->display('Seckill/index');
    }
    public function add(){
        $this->assign('title','添加秒杀');
        //查询开启秒杀活动的goods_id
        $goods_id_seck = M('goods')->where("goods_type=1 AND is_show=1")->select();
        $is_goods_ex = M('seckill')->select();
//        dump($goods_id_seck);die;
//        dump($is_good_ex);die;
        $this->assign('goods_id_seck',$goods_id_seck);//添加的id
        $this->assign('is_goods_ex',$is_goods_ex);//已存在的goods_id
        $this->display('Seckill/add');
    }
    public function adddo(){
        $goods_id = $_POST['id'];//商品id
        $num = I('post.num',0,'int');
        $data['goods_id']=$_POST['id'];//商品id
        $data['seck_pri']=$_POST['seck_pri'];//商品秒杀价格
        $data['num']=I('post.num',0,'int');//数量
        $data['is_open'] = $_POST['is_open'];//是否开启
        $data['begin_time'] = $_POST['begin_time'];//开始时间
        $data['end_time'] = $_POST['end_time'];//结束时间
        //判断开始时间/结束时间是否设置错误
        $a = strtotime($_POST['begin_time']);
        $b = strtotime($_POST['end_time']);
        if($b<$a){
            $this->error('结束时间设置错误,请重新设置',U('Seckill/index'));
        }
        $r1 =M('seckill')->where("goods_id='$goods_id'")->find();
        if($r1){
            //存在于秒杀列表时
            $this->error('商品已在秒杀列表',U('Seckill/index'));
        }else{
            //不存在于秒杀列表时
            //判断数量
            $r2 =M('goods')->where("id='$goods_id'")->find();
            $sj_num = $r2['num'];
            if($num>$r2['num']){
                $this->error("秒杀数量大于总数量,最大数量为:$sj_num",U('Seckill/add'));
            }else{
                //数量符合时
                //写入数据库
                $res = M('seckill')->add($data);
                $this->success('添加成功',U('Seckill/index'));
            }
        }
    }
    public function del(){
        $id = I('get.id',0,'int');
//        dump($id);die;
        if(M('seckill')->delete($id)>0){
            $this->success('删除秒杀成功',U('Seckill/index'));
        }else{
            $this->error('删除秒杀失败',U('Seckill/index'));
        }


    }
    public function order(){
        $this->assign('title','秒杀订单');
        $this->display('Seckill/order');
    }
}